package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.HouseListBean;
import com.csweimei.rentmanagementtreasure.http.entity.QuarterEntity;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.pop.PopHouseSx;
import com.csweimei.rentmanagementtreasure.pop.PopIntention;
import com.csweimei.rentmanagementtreasure.pop.PopQuarterChoose;
import com.csweimei.rentmanagementtreasure.pop.PopSelClick;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class HouseInfoVM extends TitleVM {

    public ObservableField<String> flag = new ObservableField<>();

    public ObservableField<String> selType = new ObservableField<>("整租");

    public ObservableField<String> keyword = new ObservableField<>("");
    public ObservableField<String> name = new ObservableField<>("");
    public ObservableField<String> city = new ObservableField<>("全部");
    public ObservableField<String> quarters = new ObservableField<>("全部");
    public ObservableField<String> housingType = new ObservableField<>("");
    public ObservableField<String> toward = new ObservableField<>("");
    public ObservableField<String> islift = new ObservableField<>("");
    public ObservableField<String> isImg = new ObservableField<>("");

    public ObservableField<String> other = new ObservableField<>("");

    public HouseInfoVM(Application mApplication, Repository mRepository) {
        super(mApplication, mRepository);
    }

    public BindingCommand back = new BindingCommand(() -> finish());

    @Override
    public void onResume() {
        super.onResume();
        if (model.isLogin())
            getData();

    }

    public ObservableField<Integer> showNoData = new ObservableField<>(View.VISIBLE);

    public ObservableList<HouseInfoPVM>  observableList = new ObservableArrayList<>();
    public ItemBinding<HouseInfoPVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_house_info);

    private void getData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        map.put("types",selType.get());
        map.put("name",name.get());
        map.put("city",city.get());
        map.put("quarters",quarters.get());
        map.put("housingType",housingType.get());
        map.put("toward",toward.get());
        map.put("islift",islift.get());
        map.put("isImg",isImg.get());
        map.put("keyword",keyword.get());
        model.housinglist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<HouseListBean>() {
                    @Override
                    public void onNext(HouseListBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNoData.set(View.GONE);
                                for (HouseListBean.DataDTO pBean : o.getData()) {
                                    HouseInfoPVM houseInfoPVM = new HouseInfoPVM(HouseInfoVM.this,pBean);
                                    observableList.add(houseInfoPVM);
                                }
                            }else {
                                showNoData.set(View.VISIBLE);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNoData.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public BindingCommand showxq = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getQuartersData();
        }
    });

    public BindingCommand showSx = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopHouseSx popIntention = new PopHouseSx(mActivity, new PopHouseSx.PopSelBack() {
                @Override
                public void back(String hxT, String cxT, String otherT) {
                    housingType.set(hxT);
                    toward.set(cxT);
                    if (ValidateUtils.isNotEmptyString(otherT)){
                        if (otherT.contains("电梯")){
                            if (otherT.equals("有电梯")){
                                islift.set("1");
                            }else {
                                islift.set("2");
                            }
                        }

                        if (otherT.contains("照片")){
                            if (otherT.equals("有照片")){
                                isImg.set("1");
                            }else {
                                isImg.set("2");
                            }
                        }

                    }else {
                        islift.set("");
                        isImg.set("");
                    }


                    other.set(otherT);

                    onResume();
                }
            });
            PopUtils.showPop(mActivity,popIntention,false);
            popIntention.setChoose(housingType.get(),toward.get(),other.get());
        }
    });


    private void getQuartersData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        model.get_quarters(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<QuarterEntity>() {
                    @Override
                    public void onNext(QuarterEntity bean) {
                        if (bean.issucc){
                            showPop(bean.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void showPop(List<QuarterEntity.DataDTO> data) {
        List<String> list = new ArrayList<>();
        list.add("全部");
        for (QuarterEntity.DataDTO bean:data){
            list.add(bean.getQuarters());
        }

        PopQuarterChoose popQuarters = new PopQuarterChoose(mActivity, list,new PopSelClick() {
            @Override
            public void sel(PopBean x) {
                quarters.set(x.getName());
                getData();
            }
        });
        PopUtils.showAttachPop(mActivity,view,popQuarters);

    }


    LinearLayout view;
    public void setView(LinearLayout llRoot) {
        view = llRoot;
    }

    public void setHouseStatus(int id, String status){
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        map.put("sid",""+id);
        map.put("roomid","");
        map.put("status",status);
        model.housingstatic(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult bean) {
                        if (bean.issucc){
                            getData();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
